<template>
  <div class="login">
    <van-nav-bar>
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar>
    <div class="content">
      <div class="title">您好，请登录</div>
      <van-form ref="form">
        <van-field
          v-model="form.mobile"
          name="mobile"
          :rules="rules.mobile"
          placeholder="请输入手机号"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60b;</i>
          </template>
        </van-field>
        <van-field
          v-model="form.code"
          :rules="rules.code"
          placeholder="请输入验证码"
        >
          <template #left-icon>
            <i class="iconfont">&#xe60c;</i>
          </template>
          <template #button>
            <span class="button" @click="getCode" v-if="totalTime === 6"
              >获取验证码</span
            >
            <span class="button" v-else>{{ totalTime }}秒</span>
          </template>
        </van-field>
      </van-form>
      <div class="tip">
        登录即同意<span> 《用户使用协议》</span>和<span>《隐私协议》</span>
      </div>
      <van-button type="danger" block round @click="submit">确定</van-button>
    </div>
  </div>
</template>
<script>
import { auCode, auLogin } from '@/api/login.js'

export default {
  name: 'login',
  data () {
    return {
      // 定时器时间
      totalTime: 6,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        mobile: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'onChange'
          },
          {
            validator: (value, rules) => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '手机号格式有误',
            trigger: 'onChange'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'onChange'
          }
        ]
      }
    }
  },
  methods: {
    // 局部校验需要name属性,还有给form表单一个ref值
    // validate(name值).then(()={}).catch(()=>{})
    getCode () {
      // console.log('是呀', _fetch.defaults)
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 给验证码一个定时器功能
          // 在请求开始前
          this.totalTime--
          const _time = setInterval(() => {
            this.totalTime--
            if (this.totalTime <= 0) {
              clearInterval(_time)
              this.totalTime = 6
            }
          }, 1000)
          // 没获取到验证码之前显示在加载中
          this.$toast.loading({
            message: '加载中...',
            duration: 0
          })
          auCode({ mobile: this.form.mobile })
            .then(res => {
              console.log(res)
              // 替换掉之前的加载
              this.$toast.success(res.data.data)
              // 获取到验证码停止倒计时
              clearInterval(_time)
              this.totalTime = 6
            })
            .catch(err => {
              this.$toast.fail(err)
            })
        })
        .catch(() => {
          this.$toast.fail('失败')
        })
    },
    // 全局验证 form表单一个ref值
    // validate().then(()={}).catch(()=>{})
    submit () {
      this.$refs.form
        .validate()
        .then(() => {
          this.$toast.loading({
            message: '验证中',
            duration: 0
          })
          auLogin(this.form)
            .then(res => {
              // console.log(res)
              console.log(123)
              this.$toast.success(res.data.message)
              // 登录成功后把token保存下来
              window.localStorage.setItem('token', res.data.data.jwt)
            })
            .catch(err => {
              console.log(err)
            })
        })
        .catch(() => {
          console.log(456)
          this.$toast.fail('验证失败')
        })
    }
  },
  created () {
    // console.log('是我呀', _fetch.defaults)
    console.log(process.env.VUE_APP_URL)
  }
}
</script>
<style lang="less" scoped>
.login {
  height: 100%;
  .f44 {
    font-size: 44px;
  }
  ::v-deep .van-nav-bar__left {
    padding: 0;
  }
  .content {
    padding-left: @p15;
    .title {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Semibold;
      font-weight: 600;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
      padding: 50px 0 63px;
    }
    .iconfont {
      padding-right: 19px;
    }
    .button {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #00b8d4;
      line-height: 22px;
      letter-spacing: 0px;
    }
    .tip {
      font-size: 12px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: left;
      color: #181a39;
      line-height: 16px;
      letter-spacing: 0px;
      padding: 15px 0 44px;
    }
  }
}
</style>
